%%%-------------------------------------------------------------------
%%% File    : demetra_sup.erl
%%% Author  :  <vjache>
%%% Description : Demetra supervisor callback module.
%%%
%%% Created : 18 Oct 2008 by  <vvorobyov>
%%%-------------------------------------------------------------------
-module(dmtr_sup).

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).


%%====================================================================
%% API functions
%%====================================================================
%%--------------------------------------------------------------------
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the supervisor
%%--------------------------------------------------------------------
start_link() ->
    supervisor:start_link(?MODULE, []).

	
%%====================================================================
%% Supervisor callbacks
%%====================================================================
%%--------------------------------------------------------------------
%% Func: init(Args) -> {ok,  {SupFlags,  [ChildSpec]}} |
%%                     ignore                          |
%%                     {error, Reason}
%% Description: Whenever a supervisor is started using 
%% supervisor:start_link/[2,3], this function is called by the new process 
%% to find out about restart strategy, maximum restart frequency and child 
%% specifications.
%%--------------------------------------------------------------------
init([]) ->
	ok=error_logger:logfile({open, "./evt."++atom_to_list(node())++".log"}),
	ok=gen_event:add_handler(error_logger, svc_glog, {dispatcher,fun svc_glog:pf_any/1}),
	io:format("dmtr_sup:init~n"),
	{ok,Randezvous}=application:get_env(randezvous),
	{ok,Factories}=application:get_env(factories),
	{ok,QueueConfigList}=application:get_env(queues),
	{ok,PoolConfigList}=application:get_env(rpools),
	io:format("dmtr_sup:init 1~n"),
	ACms = {svc_cms,{svc_cms,start_link,[Randezvous,_Opts=[]]},
			permanent,2000,worker,[svc_cms]},
	ARrs = {svc_rrs,{svc_rrs,start_link,[_Opts=[]]},
			permanent,2000,worker,[svc_rrs]},
	AVms = {svc_vms,{svc_vms,start_link,[Factories,_Opts=[]]},
			permanent,2000,worker,[svc_vms]},
	AQueues = {svc_queue_sup,{svc_queue_sup,start_link,[QueueConfigList]},
			 transient, infinity, supervisor,[svc_queue_sup]},
	APools = {svc_rpool_sup,{svc_rpool_sup,start_link,[PoolConfigList]},
			 transient, infinity, supervisor,[svc_rpool_sup]},
	Base=[AQueues,APools,ACms,ARrs,AVms],
	Specs=case node() of
			  Randezvous ->
				  AGlog={svc_glog,{svc_glog,start_link,[]},
						 permanent,2000,worker,[svc_glog]},
				  [AGlog|Base];
			  _ ->
				  Base
		  end,
	{ok,{{one_for_one,0,1}, Specs}}.

%%====================================================================
%% Internal functions
%%====================================================================
